package com.android.launcher3.hotseat;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public enum CustomHotSeatManager {
    INSTANCE;

    private static final String HOTS_SEAT_KEY = "CustomHotSeat";
    private static final String SP_KEY = "SP_KEY";

    private final List<HotSeatEntity> hotSeatList = new ArrayList<>();
    private SharedPreferences sharedPref;

    public void init(Context context) {
        sharedPref = context.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE);
        load();
    }

    private void load() {
        hotSeatList.clear();

        loadList(HOTS_SEAT_KEY, hotSeatList);

    }

    private void loadList(String hotsSeatKey, List<HotSeatEntity> hotSeatList) {
        try {
            String value = sharedPref.getString(hotsSeatKey, "[]");
            JSONArray jsonArray = new JSONArray(value);

            if (jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    hotSeatList.add(new HotSeatEntity(jsonObject.optInt("index"), jsonObject.optString("packageName")));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveHotSeat(HotSeatEntity hotSeat) {
        save(hotSeat, HOTS_SEAT_KEY, hotSeatList);
    }


    private void save(HotSeatEntity hotSeat, String spKey, List<HotSeatEntity> hotSeatList) {
        if (hotSeat.index < 0) return;

        String value = "[]";
        HotSeatEntity findHotSeatEntity = findHotSeatEntity(hotSeat, hotSeatList);

        if (findHotSeatEntity != null) {
            findHotSeatEntity.packageName = hotSeat.packageName;
        } else {
            hotSeatList.add(hotSeat);
        }

        if (!hotSeatList.isEmpty()) {
            JSONArray jsonArray = getJsonArray(hotSeatList);
            value = jsonArray.toString();
        }

        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(spKey, value);
        editor.apply();
    }

    private JSONArray getJsonArray(List<HotSeatEntity> list) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            HotSeatEntity hotSeatEntity = list.get(i);
            if (hotSeatEntity != null) {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("index", hotSeatEntity.index);
                    jsonObject.put("packageName", hotSeatEntity.packageName);
                    jsonArray.put(jsonObject);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return jsonArray;
    }

    public static class HotSeatEntity {

        public int index;
        public String packageName;

        public HotSeatEntity(int index, String packageName) {
            this.index = index;
            this.packageName = packageName;

        }
    }


    public List<HotSeatEntity> getHotSeatList() {
        return hotSeatList;
    }


    private HotSeatEntity findHotSeatEntity(@NonNull HotSeatEntity hotSeatEntity, @NonNull List<HotSeatEntity> data) {
        for (int i = 0; i < data.size(); i++) {

            HotSeatEntity hotSeat = data.get(i);

            if (hotSeat.index == hotSeatEntity.index) {
                return hotSeat;
            }
        }

        return null;

    }

}


